package com.zhengdongzulin.zuke.app.main.home


import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.androidktx.core.*
import com.zhengdongzulin.zuke.R
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import com.zhengdongzulin.zuke.app.data.City
import com.zhengdongzulin.zuke.app.data.HomeItem
import com.zhengdongzulin.zuke.app.data.HotRoom
import com.zhengdongzulin.zuke.app.main.ZKWebviewActivity
import com.zhengdongzulin.zuke.app.main.apartment.ZKStoreDetailsActivity
import com.zhengdongzulin.zuke.app.main.findhouse.ZKFindHouseActivity
import com.zhengdongzulin.zuke.app.main.findhouse.ZKRoomDetailsActivity
import com.zhengdongzulin.zuke.app.main.home.map.ZKMapFindHouseActivity
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkFragmentHomeBinding
import com.zhengdongzulin.zuke.ext.loadImage
import com.zhengdongzulin.zuke.ext.put
import com.zhengdongzulin.zuke.ext.start

/**
 * jeff
 * 首页
 * 2021-05-18 11:01:51
 */
class ZKHomeFragment : BaseFragment<HomeVm, ZkFragmentHomeBinding>() {

    companion object {
        fun newInstance() = ZKHomeFragment()
    }

    lateinit var mListener: (type: String) -> Unit

    override fun getLayoutResId(): Int = R.layout.zk_fragment_home

    override fun initVM(): HomeVm = getVM(HomeVm::class.java)

    override fun onResume() {
        super.onResume()
        if (mShareViewModel.apartmentTypeIsChangeForHome.value != null && mShareViewModel.apartmentTypeIsChangeForHome.value!!) {
            initView()
        }
    }

    override fun initView() {
        var city = sp().getString("current_city", "")
        if (city.isNullOrEmpty()) {
            mViewModel.get_city_com_list()
        } else {
            var mCity = city.toBean<City>()
            mCity?.let {
                mViewModel.get_banner_pic_list(it.cityId)
                mViewModel.get_house_item_list(it.cityId)
                mViewModel.get_hot_house_list(it.cityId)
                mView.tvCity.setText(it.cityName)
                mShareViewModel.city.postValue(it)
            }
        }
    }

    override fun initData() {

    }

    fun OnMenuClick(listener: (type: String) -> Unit) {
        mListener = listener
    }

    override fun initListener() {
        mView.llPersonnelApartment.setOnClickListener {
            mListener("公寓")
        }
        mView.tvEmptyMore.visibility = View.GONE
        mView.tvEmptyMore.setOnClickListener {
            Intent().start(requireActivity(), ZKFindHouseActivity::class.java)
        }
//        select_city.setOnClickListener {
//            Intent().start(requireActivity(),SelectCityActivity::class.java)
//        }
        mView.llApartment.setOnClickListener {
            mListener("公寓")
        }
//        house_type1.setOnClickListener {
//            mListener("整租")
//        }
//        house_type2.setOnClickListener {
//            mListener("合租")
//        }
        mView.refreshLayout.setOnRefreshListener {
            val cityId = mShareViewModel?.city?.value?.cityId
            mViewModel.get_banner_pic_list(cityId)
            mViewModel.get_house_item_list(cityId)
            mViewModel.get_hot_house_list(cityId)
        }
        mView.map.setOnClickListener {
//            toast("敬请期待")
            Intent().start(requireContext(), ZKMapFindHouseActivity::class.java)
        }
        mView.back.setOnClickListener {
            requireActivity().finish()
        }
//        cl_apartment_type.setOnClickListener {
//            Intent().start(requireActivity(), ApartmentTypeActivity::class.java)
//        }
    }

    override fun startObserve() {
        mViewModel.get_city_com_list.observe(this, Observer {
            val city = it[0]
            sp().putString("current_city", city.toJson())
            sp().putString("city", it.toList().toJson())
            mView.tvCity.setText(city.cityName)
            mShareViewModel.city.value = city
            mShareViewModel.city.postValue(city)
            mViewModel.get_banner_pic_list(city.cityId)
            mViewModel.get_house_item_list(city.cityId)
            mViewModel.get_hot_house_list(city.cityId)
        })
        mViewModel.get_city_com_list.state.observe(this, getObserver(mViewModel.get_city_com_list))

        mViewModel.get_banner_pic_list.observe(this, Observer {
            var imagesData = mutableListOf<BannerItem>()
            for (banner in it) {
                imagesData.add(BannerItem().setImgUrl(banner.picUrl))
            }
            mView.banner.setSource(imagesData)?.startScroll()
            mView.banner.setOnItemClickListener { view, item, position ->

                if (!it[position].picBindLink.isEmpty()) {
                    Intent().put("url", it[position].picBindLink)
                        .start(this.requireContext(), ZKWebviewActivity::class.java)
                }

            }
        })
        mViewModel.get_banner_pic_list.state.observe(
            this,
            getObserver(mViewModel.get_banner_pic_list)
        )

        mViewModel.get_house_item_list.observe(this, Observer {
            val vListAdpater = VListAdpater(it)
            vListAdpater.setOnItemClickListener { adapter, view, position ->
                Intent().put("id", it[position].id)
                    .start(
                        requireActivity(),
                        ZKStoreDetailsActivity::class.java
                    )
            }
            mView.rvRecommend.vertical().adapter = vListAdpater
        })
        mViewModel.get_house_item_list.state.observe(
            this,
            getObserver(mViewModel.get_house_item_list)
        )

        mViewModel.get_hot_house_list.observe(this, Observer {
            val hListAdpater = HListAdpater(it)
            hListAdpater.setOnItemClickListener { adapter, view, position ->
                Intent().put("id", it[position].houseId).start(
                    requireActivity(),
                    ZKRoomDetailsActivity::class.java
                )
            }
            mView.rvSelected.horizontal().adapter = hListAdpater
            mView.refreshLayout.finishRefresh()
        })
        mViewModel.get_hot_house_list.state.observe(
            this,
            getObserver(mViewModel.get_hot_house_list)
        )
    }


    inner class HListAdpater(
        list: MutableList<HotRoom>
    ) : BaseQuickAdapter<HotRoom, BaseViewHolder>(
        R.layout.item_home_selected,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: HotRoom) {
            holder.loadImage(R.id.photo, item.picUrl, R.mipmap.error_empty_zheng, 5f)
            holder.setText(R.id.name, item.houseName)
            holder.setText(
                R.id.desc,
                "${item.doorName}\u0020\u0020${item.area}㎡\u0020\u0020${item.orientation}"
            )
            var money = holder.getView<TextView>(R.id.money)
            if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                money.text = ""
                money.appendStyleSpan("面议")
            } else {
                money.text = "￥${item.rentPrice}"
                money.appendSizeSpan("/月", 0.46f)
                    .colorSpan(
                        "",
                        money.text.length - 2..money.text.length,
                        context.color(R.color.black_66)
                    )
                    .styleSpan("", 0..money.text.length - 2)
            }
        }
    }

    inner class VListAdpater(
        list: MutableList<HomeItem>
    ) : BaseQuickAdapter<HomeItem, BaseViewHolder>(
        R.layout.item_home_recommend,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: HomeItem) {
            holder.loadImage(R.id.iv_1, item.firstItemPicUrl, R.mipmap.error_empty_zheng, 5f)
            holder.loadImage(R.id.iv_2, item.secondItemPicUrl, R.mipmap.error_empty_zheng, 5f)
            holder.loadImage(R.id.iv_3, item.thirdItemPicUrl, R.mipmap.error_empty_zheng, 5f)
            holder.setText(R.id.name, item.houseItemName)
            holder.setText(R.id.location, item.houseItemAddress)
            var money = holder.getView<TextView>(R.id.money)
            if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                money.text = ""
                money.appendStyleSpan("面议")
            } else {
                money.text = "￥${item.minMoney}"
                money.appendSizeSpan("/月", 0.46f)
                    .colorSpan(
                        "",
                        money.text.length - 2..money.text.length,
                        context.color(R.color.black_66)
                    )
                    .styleSpan("", 0..money.text.length - 2)
            }
        }
    }

}